package com.resys.server.controller.customer;

import com.resys.annotation.GlobalInterceptor;
import com.resys.entity.dto.OrderDto;
import com.resys.entity.dto.TokenCustomerDto;
import com.resys.entity.enums.DateTimePatternEnum;
import com.resys.entity.enums.ResponseCodeEnum;
import com.resys.entity.po.Meetingroom;
import com.resys.entity.po.Reservationorder;
import com.resys.entity.vo.OrdersVo;
import com.resys.entity.vo.ResponseVO;
import com.resys.exception.BusinessException;
import com.resys.server.controller.ABaseController;
import com.resys.service.MeetingroomService;
import com.resys.service.ReservationorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.LocalDate;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController("ordersController")
@Api(tags = "客户模块-下单接口")
@RequestMapping("/customer/orders")
public class OrdersController extends ABaseController {
         @Resource
         private ReservationorderService reservationService;
         @Resource
         private MeetingroomService meetingroomService;
         @ApiOperation("预约下单请求")
         @PostMapping("/add")
         @GlobalInterceptor(checkLogin = true)
         public ResponseVO addOrder(@RequestBody OrderDto orderDto) {
             LocalDate start = LocalDate.fromDateFields(orderDto.getStartDate());
             if(start.minusDays(60).isAfter(LocalDate.now())){
                 throw new BusinessException(ResponseCodeEnum.CODE_902);
             }
             TokenCustomerDto tokenCustomerDto = getTokenCustomer();
              if(tokenCustomerDto == null){
                  throw new BusinessException(ResponseCodeEnum.CODE_901);
              }
              Meetingroom room = meetingroomService.getMeetingroomByRoomId(orderDto.getRoomId());
              Integer hours = (int) ((orderDto.getEndDate().getTime() - orderDto.getStartDate().getTime()) / 1000 / 60 / 60);
              BigDecimal price = room.getPricePerHour().multiply(new BigDecimal(hours));
              if(room.getStatus()!=0){
                  throw new BusinessException("该会议室已被抢购");
              }
              room.setStatus(1);
              meetingroomService.updateMeetingroomByRoomId(room, room.getRoomId());
              Reservationorder reservationorder = new Reservationorder();
              reservationorder.setCustomerId(tokenCustomerDto.getCustomerId());
              reservationorder.setCreatedAt(new Date());
              reservationorder.setRoomId(orderDto.getRoomId());
              reservationorder.setStartTime(orderDto.getStartDate());
              reservationorder.setEndTime(orderDto.getEndDate());
              reservationorder.setStatus(0);
              Date now= new Date();
              now.setMinutes(now.getMinutes()+30);
              reservationorder.setLockedUntil(now);
              reservationorder.setTotalPrice(price);
              reservationorder.setParticipantsNum(orderDto.getNum());
              List<Reservationorder> reservationorderList = new ArrayList<>();
              reservationorderList.add(reservationorder);
              reservationService.addBatch(reservationorderList);
              OrdersVo reslut = new OrdersVo();
              BeanUtils.copyProperties(reservationorderList.get(0), reslut);
              reslut.setRoomName(room.getName());
              return getSuccessResponseVO(reslut);
         }
         @ApiOperation("支付")
         @PostMapping("/confirm")
         @GlobalInterceptor(checkLogin = true)
         public ResponseVO confirmOrder(Integer orderId) {
             Reservationorder reservationorder = reservationService.getReservationorderByOrderId(orderId);
             if (reservationorder == null){
                 throw new BusinessException("订单不存在，支付失败");
             }
             if(new Date().after(reservationorder.getLockedUntil())){
                 throw new BusinessException("订单已过期，支付失败");
             }
             if(reservationorder.getStatus() == 1)
              {throw new BusinessException("订单已支付，请勿重复支付");}
             reservationorder.setStatus(1);
             reservationorder.setPaymentTime(new Date());
             reservationService.updateReservationorderByOrderId(reservationorder, orderId);
             Meetingroom room =  meetingroomService.getMeetingroomByRoomId(reservationorder.getRoomId());
             room.setStatus(2);
             meetingroomService.updateMeetingroomByRoomId(room, room.getRoomId());
             return getSuccessResponseVO(reservationorder);
         }
         @ApiOperation("预约系统派单")
         @PostMapping("/dispatch")
         @GlobalInterceptor(checkLogin = true)
             public ResponseVO dispatchOrder(@RequestBody OrderDto orderDto){
             LocalDate start = LocalDate.fromDateFields(orderDto.getStartDate());
             if(start.minusDays(60).isAfter(LocalDate.now())){
                 throw new BusinessException(ResponseCodeEnum.CODE_902);
             }
             TokenCustomerDto tokenCustomerDto = getTokenCustomer();
             if(tokenCustomerDto == null){
                 throw new BusinessException(ResponseCodeEnum.CODE_901);
             }
             OrderDto order =  meetingroomService.dispatchOrder(orderDto);
             Meetingroom room = meetingroomService.getMeetingroomByRoomId(order.getRoomId());
             Integer hours = (int) ((orderDto.getEndDate().getTime() - orderDto.getStartDate().getTime()) / 1000 / 60 / 60);
             BigDecimal price = room.getPricePerHour().multiply(new BigDecimal(hours));
             if(room.getStatus()==0){
                 room.setStatus(1);
                 meetingroomService.updateMeetingroomByRoomId(room, room.getRoomId());
             }
             Reservationorder reservationorder = new Reservationorder();
             reservationorder.setCustomerId(tokenCustomerDto.getCustomerId());
             reservationorder.setCreatedAt(new Date());
             reservationorder.setRoomId(order.getRoomId());
             reservationorder.setStartTime(order.getStartDate());
             reservationorder.setEndTime(order.getEndDate());
             reservationorder.setStatus(0);
             Date now= new Date();
             now.setMinutes(now.getMinutes()+30);
             reservationorder.setLockedUntil(now);
             reservationorder.setTotalPrice(price);
             reservationorder.setParticipantsNum(order.getNum());
             List<Reservationorder> reservationorderList = new ArrayList<>();
             reservationorderList.add(reservationorder);
             reservationService.addBatch(reservationorderList);
             OrdersVo reslut = new OrdersVo();
             BeanUtils.copyProperties(reservationorderList.get(0), reslut);
             reslut.setRoomName(room.getName());
             return getSuccessResponseVO(reslut);
         }


}
